var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    step: 1,
    img_id_card_front: "/images/upload-add.svg",
    img_id_card_back: "/images/upload-add.svg",
    img_id_card_Handheld: "/images/upload-add.svg",
    img_business_license: "/images/upload-add.svg",
    name: "",
    id_card: "",
    CO_NAME: "",
    CO_CODE: "",
    btn_text: "返回"
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    this.setData({
      nofuser: app.globalData.user_id == 1 ? false : true
    })
  },
  onReady: function () {
    wx.hideToast()
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        wx.showLoading({
          title: '上传中...',
          mask: true
        })

        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var item = String(e.currentTarget.id), item_value = {};
        item_value[item] = res.tempFilePaths[0];
        that.setData(item_value);
        wx.uploadFile({
          url: app.server + '/wechat/passport/uploadCard_enterprise',
          header:{
            'Authorization': app.globalData.token
          },
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'CO_ID': app.globalData.CO_ID,
            'user_id': app.globalData.user_id,
            'item': item
          },
          success: function (res) {
            var data = JSON.parse(res.data);
            console.log(data)
            if (data.success) {
              if (item == 'img_id_card_front') {
                that.setData({
                  'name': data.name,
                  'id_card': data.id_card_number
                });
              }
              if (item == 'img_business_license') {
                that.setData({
                  'CO_NAME': data.CO_NAME,
                  'CO_CODE': data.CO_CODE
                });
              }
              var upload_item = String(e.currentTarget.id), item_value = {};
              var path = data.data[0].path;
              if (path.indexOf("http") == -1) {
                path = app.server + path
              }
              item_value[upload_item] = path;
              that.setData(item_value);
            } else {
              wx.showModal({
                content: data.data,
                confirmText: "确定",
                showCancel: false
              })
            }
          },
          complete: function () {
            wx.hideLoading()
          }
        })
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  submitForm: function (e) {
    var that = this;
    // var verify = false;
    // if (that.data.id_card != '' && that.data.CO_CODE != '') {
    //   verify = true;
    // }
    wx.showLoading({
      title: '请稍候...',
    });
    // if (!verify) {
    //   var verifyText= new Array(),vi=0;
    //   // if (that.data.id_card == ''){
    //   //   verifyText[vi] = '身份证未验证';
    //   //   vi++;
    //   // }
    //   if (that.data.CO_CODE == '') {
    //     verifyText[vi] = '营业执照未验证';
    //     vi++;
    //   }
    //   wx.hideLoading();
    //   wx.showModal({
    //     title: '温馨提示',
    //     content: verifyText.join(","),
    //     cancelText:'返回',
    //     confirmText:'继续上传',
    //     success: function (res) {
    //       if (res.confirm) {

    //       }else{
    //         wx.redirectTo({
    //           url: '/pages/index/index?reload=true'
    //         })
    //       }
    //     }
    //   })
    //   return false;
    // }
    if (!that.data.CO_NAME) {
      wx.showToast({
        title: '请输入企业名称',
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    if (!that.data.CO_CODE) {
      wx.showToast({
        title: '请输入统一代码',
        image: '/images/wrong.png',
        duration: 3000
      });
      return false;
    }
    util.postData("/wechat/passport/regEnterprise", {
      CO_NAME: that.data.CO_NAME,
      CO_CODE: that.data.CO_CODE,
      ADD_USER: app.globalData.user_id
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        wx.showModal({
          title: '温馨提示',
          content: '恭喜你，注册成功！',
          success: function (res) {
            wx.redirectTo({
              url: '/pages/index/index'
            })
          }
        })
      } else if (res.data.success == false) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000,
          mask: true
        });
      }
    });
  }
});